$(document).ready(function(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	/***************************    选择行业    ***************************/
	if($("#fatherId").val() == "" && $("#industryId").val() != ""){
		$('#choiceDiv_dialog').css("display","block");
		$('.single_div_radio').prop("type","checkbox");
		$('.industry_oper').val("上一步");
		parent.layer.title("选择行业");
		$('.industry_save').removeClass("industry_change");
	}
	if(($("#fatherId").val() == ""  && $("#industryId").val() == "" ) || ($("#fatherId").val() != "" && $("#industryId").val() == "")){
		$('.industry_oper').val("下一步");
		parent.layer.title("选择父行业");
		$('.industry_save').addClass("industry_change");
	}
	if((parent.$('#industryFatherId').val() == "" || parent.$('#industryFatherId').val() == 0)
			&& parent.$('#industryId').val() != ""){
		$('.industry_oper').addClass("industry_change");
		$('.industry_save').removeClass("industry_change");
	}
	
	$(".industry_oper").click(function(){
		if($(this).val() == "下一步"){
			var industryId = $("#selectIndustryDiv input[name='industry']:checked").val();
			var industryName = $("#selectIndustryDiv input[name='industry']:checked").attr("title");
			if(industryId == undefined){
				if(parent.$('#industryFatherId').val() != ""){
					industryId = parent.$('#industryFatherId').val();
					industryName = parent.$('#industryName').val();
				}else{
					alert("请选择父行业之后，在进行下一步操作！");
					return false;
				}
			}
			parent.$('#industryFatherId').val(industryId);
			parent.$('#industryName').val(industryName);
			$("#fatherId").val(parseInt(industryId));
			$("#selectIndustryForm").attr("action", "getIndustry");
			$('#selectIndustryForm').submit();
		}else if($(this).val() == "上一步"){
			$("#selectIndustryForm").attr("action", "getAllFatherIndustry");
			$('#selectIndustryForm').submit();
		}
	});
	
	$(".industry_save").click(function(){
		var parentsindustry2=parent.$('#industryName').val();
		var industryId2="";
		var industryName2="";
		$("#selectIndustryDiv input[name='industry']:checked").each(function(){
			industryId2+=$(this).val()+",";
			industryName2+=$(this).attr("title")+","
		});
		var industryId = industryId2.slice(0,-1);
		var industryName = industryName2.slice(0,-1);
		if(industryId == undefined||industryId == ""){
			if(parent.$('#industryId').val() != ""){
				industryId = parent.$('#industryId').val();
				industryName = parent.$('#industryName').val();
			}else{
				alert("请选择行业！");
				return false;
			}
		}
		parent.$('#industryId').val(industryId);
		parent.$('#industryName').val(industryName);
		parent.$("#selectIndustry_span").text("已选择 —— "+ parentsindustry2);
		parent.$("#industryul_selectdiv").find('span.spanVaildate').html("");
		parent.layer.close(index);//再执行关闭

	});
	$("#selectIndustryDiv input[name='industry']").each(function(){
		if($(this).val() == $('#fatherId').val() || $(this).val() == $('#industryId').val() || 
				$(this).val() == parent.$('#industryFatherId').val()){
			$(this).attr('checked',true);
			judgeIssave($(this).val());
		}
		if($(this).val() == parent.$('#industryId').val()){
			$(this).attr('checked',true);
		}
	});
	var parentIdss=parent.$('#industryId').val().split(",");
	for(var i=0;i<parentIdss.length;i++){
		$("#selectIndustryDiv input[name='industry']").each(function(){
			if($(this).val() == parentIdss[i]){
				$(this).attr('checked',true);
			}
		});
	}
	
	$("#selectIndustryDiv input[name='industry']").click(function(){
		if($(".industry_oper").val() == "下一步"){
			$('#industryId').val($(this).val());
			$("#fatherId").val($(this).val());
			parent.$('#industryId').val("");
			parent.$('#industryFatherId').val("");
		}else if($(".industry_oper").val() == "上一步"){
			$("#fatherId").val($(this).val());
		}
		judgeIssave($(this).val());
	});
	
	// 分页中超链接跳转


	$('#selectIndustryForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectIndustryForm').find('input:hidden[name="page"]').val(_page);
		$('#selectIndustryForm').submit();
	});
	
	//全选按钮点击
	$(".industry_checkall").click(function(){
		$('.industry_oper').addClass("industry_change");
		$('#selectIndustryDiv .single_div input[type="checkbox"]').prop("checked","checked");
	});
    //取消选中按钮点击

	$(".industry_cancleall").click(function(){
		$('#selectIndustryDiv .single_div input[type="checkbox"]').prop("checked",false);
	})
});

function judgeIssave(fatherId){
	$.post('isExistIndustry',{fatherId:fatherId},function(data){
		if(data == false){
			$('.industry_oper').removeClass("industry_change");
			$('.industry_save').addClass("industry_change");
		}else{
			$('.industry_save').removeClass("industry_change");
			$('.industry_oper').addClass("industry_change");
		}
	});
}